//
//  StapleTableViewCell.m
//  美食百科
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "StapleTableViewCell.h"
#import "ClassModel.h"
#import "UIImageView+WebCache.h"
#import "CollectModel.h"
#define kWidth self.contentView.frame.size.width
#define kHeight self.contentView.frame.size.height
@interface StapleTableViewCell()
@property(nonatomic,retain)UIImageView *foodImage;
@property(nonatomic,retain)UILabel *nameLabel;
@property(nonatomic,retain)UILabel *caloriesLabel;
@property(nonatomic,retain)UIImageView *levelImage;


@end
@implementation StapleTableViewCell
- (void)dealloc
{   [_classModel release];
    [_collect release];
    [_string release];
    [_foodImage release];
    [_nameLabel release];
    [_caloriesLabel release];
    [_levelImage release];
    [super dealloc];
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        self.foodImage = [[UIImageView alloc]init];
       // self.foodImage.backgroundColor = [UIColor orangeColor];
        
        [self.contentView addSubview:self.foodImage];
        [_foodImage release];
        
        self.nameLabel = [[UILabel alloc]init];
        self.nameLabel.textColor = self.color;

      //  self.nameLabel.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:self.nameLabel];
        [_nameLabel release];
        
        self.caloriesLabel = [[UILabel alloc]init];
      //  self.caloriesLabel.backgroundColor = [UIColor redColor];
//        self.caloriesLabel.textColor = self.color;
        self.caloriesLabel.textColor = [UIColor redColor];
        [self.contentView addSubview:self.caloriesLabel];
        [_caloriesLabel release];
        
        self.levelImage = [[UIImageView alloc]init];
       // self.levelImage.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:self.levelImage];
        [_levelImage release];
        
    }
    
    return self;

}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.foodImage.frame = CGRectMake(20, 10,kWidth/6, kWidth/6);
    self.foodImage.layer.cornerRadius = self.foodImage.frame.size.width/2;
    self.foodImage.layer.masksToBounds = YES;

    self.nameLabel.frame = CGRectMake(self.foodImage.frame.origin.x+self.foodImage.frame.size.width+20, self.foodImage.frame.origin.y, kWidth/2, (kHeight-self.foodImage.frame.origin.y*2)/2);
    self.caloriesLabel.frame = CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y+self.nameLabel.frame.size.height, self.nameLabel.frame.size.width, self.nameLabel.frame.size.height);
    self.levelImage.frame = CGRectMake(kWidth-self.foodImage.frame.size.width, self.foodImage.frame.origin.y, self.foodImage.frame.size.width, self.foodImage.frame.size.height);

}

//收藏页Model赋值
-(void)setCollect:(CollectModel *)collect
{
    [self.foodImage sd_setImageWithURL:[NSURL URLWithString:collect.thumb_image_url]placeholderImage:[UIImage imageNamed:@"u=1023456699,842778947&fm=21&gp=0"]];
    self.nameLabel.text = collect.name;
    
    self.caloriesLabel.text = [NSString stringWithFormat:@"%ld大卡/100克",collect.calory];
    //找到关键字在标题内的范围
    NSRange range = [self.caloriesLabel.text rangeOfString:@"大卡/100克"];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.caloriesLabel.text];
    
    //    [attr setAttributes:@{NSBackgroundColorAttributeName:[UIColor redColor]} range:range];
    [str addAttribute:NSForegroundColorAttributeName value:self.color range:range];
    
    self.caloriesLabel.attributedText = str;

//    self.caloriesLabel.text = [NSString stringWithFormat:@"%ld大卡/100克",collect.calory];
}

//列表,搜索页Model赋值

-(void)setClassModel:(ClassModel *)classModel
{
    [self.foodImage sd_setImageWithURL:[NSURL URLWithString:classModel.thumb_image_url]placeholderImage:[UIImage imageNamed:@"u=1023456699,842778947&fm=21&gp=0"]];
    if (self.calory == 4) {
        
        self.nameLabel.text = classModel.name;
        
        //找到关键字在标题内的范围
        NSRange range = [self.nameLabel.text rangeOfString:self.string];
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.nameLabel.text];
        
        //    [attr setAttributes:@{NSBackgroundColorAttributeName:[UIColor redColor]} range:range];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        
        self.nameLabel.attributedText = str;

//        self.caloriesLabel.text = [NSString stringWithFormat:@"%@大卡/100克",classModel.CALayer
//                                   ];
//        
//        //找到关键字在标题内的范围
//        NSRange rangeOne = [self.caloriesLabel.text rangeOfString:@"大卡/100克"];
//        
//        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.caloriesLabel.text];
//        
//        //    [attr setAttributes:@{NSBackgroundColorAttributeName:[UIColor redColor]} range:range];
//        [str addAttribute:NSForegroundColorAttributeName value:self.color range:range];
//        
//        self.caloriesLabel.attributedText = str;
        
    }else
    {
        self.nameLabel.text = classModel.name;

    }
        
    if (self.calory == 0 ||self.calory == 1) {
       // NSLog(@"🍍%ld",self.calory);
        self.caloriesLabel.text = [NSString stringWithFormat:@"%@大卡/100克",classModel.calory];
        
        //找到关键字在标题内的范围
        NSRange range = [self.caloriesLabel.text rangeOfString:@"大卡/100克"];
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.caloriesLabel.text];
        
        //    [attr setAttributes:@{NSBackgroundColorAttributeName:[UIColor redColor]} range:range];
        [str addAttribute:NSForegroundColorAttributeName value:self.color range:range];
        
        self.caloriesLabel.attributedText = str;
//    self.caloriesLabel.text = [NSString stringWithFormat:@"%@大卡/100克",classModel.calory];
    }else if(self.calory == 2)
    {
    self.caloriesLabel.text = [NSString stringWithFormat:@"%@克/100克",classModel.protein];
        
        //找到关键字在标题内的范围
        NSRange range = [self.caloriesLabel.text rangeOfString:@"克/100克"];
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.caloriesLabel.text];
        
        //    [attr setAttributes:@{NSBackgroundColorAttributeName:[UIColor redColor]} range:range];
        [str addAttribute:NSForegroundColorAttributeName value:self.color range:range];
        
        self.caloriesLabel.attributedText = str;

        
//        self.caloriesLabel.text = [NSString stringWithFormat:@"%@克/100克",classModel.protein];
    }else
    {
        self.caloriesLabel.text = [NSString stringWithFormat:@"%@大卡/100克",classModel.calory];
        
        //找到关键字在标题内的范围
        NSRange range = [self.caloriesLabel.text rangeOfString:@"大卡/100克"];
        
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.caloriesLabel.text];
        
        //    [attr setAttributes:@{NSBackgroundColorAttributeName:[UIColor redColor]} range:range];
        [str addAttribute:NSForegroundColorAttributeName value:self.color range:range];
        
        self.caloriesLabel.attributedText = str;
        
//        self.caloriesLabel.text = [NSString stringWithFormat:@"%@克/100克",classModel.fat];
    }
    
    
    if ([classModel.health_light integerValue] == 1) {
        self.levelImage.image = [UIImage imageNamed:@"health1"];
    }else if ([classModel.health_light integerValue]== 2){
        self.levelImage.image = [UIImage imageNamed:@"health2"];
    }else
    {
        self.levelImage.image = [UIImage imageNamed:@"health3"];
    }
    
    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
